import { Dimensions } from "@sapi-game/constants";
import { Game, GameComponent } from "@sapi-game/main";
import { PKPreParingState } from "../state";

export class actionBarComponent extends GameComponent<PKPreParingState> {
    override onAttach(): void {
        this.subscribe(Game.events.interval, this.showActionBar.bind(this));
    }

    showActionBar() {
        const players = Dimensions.Overworld.getPlayers(
            this.state.region.getEntityQueryOption()
        );
        const blue = this.state.blueTeam.validSize;
        const red = this.state.redTeam.validSize;

        let text: string;

        if (this.state.isCounting) return;
        if (this.state.checkPlayers()) {
            text = `§b蓝队：${blue}  §c红队: ${red} §7| §e等待开始...`;
        } else {
            text = `§b蓝队：${blue}  §c红队: ${red} §7| §e等待中...`;
        }

        players.forEach((p) => p.onScreenDisplay.setActionBar(text));
    }
}
